﻿using System;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Data;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Plugins.General.NotificationIcon {
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class NotificationIconConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = true;

        public NotificationIconConfigurator() {
            InitializeComponent();

            this.cSingleClick.SelectionChanged += new SelectionChangedEventHandler(IconClick_SelectionChanged);
            this.cSingleClickCommand.TextChanged += new TextChangedEventHandler(SingleClickCommand_TextChanged);
            this.cDoubleClick.SelectionChanged += new SelectionChangedEventHandler(IconClick_SelectionChanged);
            this.cDoubleClickCommand.TextChanged += new TextChangedEventHandler(DoubleClickCommand_TextChanged);
            this.cContextMenu.TextChanged += new TextChangedEventHandler(ContextMenu_TextChanged);
        }
        
        private void SingleClickCommand_TextChanged(object sender, TextChangedEventArgs e) {
            this._Config.SetString("Notification Icon", "Single Click Command", this.cSingleClickCommand.Text);
        }
        private void DoubleClickCommand_TextChanged(object sender, TextChangedEventArgs e) {
            this._Config.SetString("Notification Icon", "Double Click Command", this.cDoubleClickCommand.Text);
        }
        private void IconClick_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            ComboBox cb = sender as ComboBox;
            int index = cb.SelectedIndex;

            switch (cb.Name) {
                case "cSingleClick":
                    this._Config.SetInt("Notification Icon", "Single Click", index);
                    break;
                case "cDoubleClick":
                    this._Config.SetInt("Notification Icon", "Double Click", index);
                    break;
            }
        }
        private void ContextMenu_TextChanged(object sender, TextChangedEventArgs e) {
            this._Config.SetString("Notification Icon", "Context Menu Items", this.cContextMenu.Text);
        }

        #region IConfigurator<IniFile> Members
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            int iconClick = this._Config.GetInt("Notification Icon", "Single Click", 1);
            iconClick = iconClick.MaxMin(0, 4);
            this.cSingleClick.SelectedIndex = iconClick;
            this.cSingleClickCommand.Text = this._Config.GetString("Notification Icon", "Single Click Command", string.Empty);

            iconClick = this._Config.GetInt("Notification Icon", "Double Click", 2);
            iconClick = iconClick.MaxMin(0, 4);
            this.cDoubleClick.SelectedIndex = iconClick;
            this.cDoubleClickCommand.Text = this._Config.GetString("Notification Icon", "Double Click Command", string.Empty);

            this.cContextMenu.Text = this._Config.GetString("Notification Icon", "Context Menu Items", NotificationIcon.DefaultContextMenuItems);

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
